Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
"object is not extensible" while accessed via `import()` function

I'm importing my JavaScript "module" using the "dynamic import()" call:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"><title>Module</title><style>:root { background: #545454; }</style>
  <script>
    window.addEventListener('load', e => {
      import('./script.js').then(async module => {
        const fn = await module.myFunction();
        fn.whatever();
      });
    });
  </script>
</head>
<body></body></html>

and, this is the script.js file:

export async function myFunction() {
    this.whatever = function () {
        console.log('Ok');
    };
    return this;
}

By running it, Google Chrome complains that the "object is not extensible" while adding whatever:

script.js:2 Uncaught (in promise) TypeError: Cannot add property whatever, object is not extensible
    at Module.myFunction (script.js:2:19)
    at module.html:12:33

So, what's wrong and how to work around the issue?

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

module in the then callback on that import() is the module namespace object for the module. That's an object the JavaScript engine creates that has properties for the exports of the module. The MNO is not extensible; its purpose is to accurately reflect the exports of the module. If you could add properites to it (extend it), it wouldn't accurately reflect the module exports anymore.

When you call module.myFunction(), this during the call is set to module during the call — which is the non-extensible module namespace object. So the line this.whatever = ___ in myFunction fails, because that's trying to create a new property on a non-extensible object.


Just as an addition: It's not clear what you're trying to do with myFunction, but you could create a new object that uses the MNO as its prototype, and add a property to that.

export async function myFunction() {
    const obj = Object.create(this);
    obj.whatever = function () {
        console.log("Ok");
    };
    return obj;
}

I wouldn't recommend doing that without a good reason (and I'd remove the async unless the function uses await), but it would work.

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!